//页面初始化
mui.init();
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var mask = mui.createMask(); //遮罩层
var city_leaber = "86";
var cityPicker3 = null;
var user_phone, is_realname; //公共变量
/*三级联动*/
(function($, doc) {
	$.init();
	$.ready(function() {
		/**
		 * 获取对象属性的值
		 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
		 * @param {Object} obj 对象
		 * @param {String} param 属性名
		 */
		var _getParam = function(obj, param) {
			return obj[param] || '';
		};
		if(city_leaber == "86" || city_leaber == "00886") {
			cityPicker3 = new $.PopPicker({
				layer: 3
			});
			cityPicker3.setData(cityData3);
			var showCityPickerButton = doc.getElementById('region');
			var cityResult3 = doc.getElementById('region');
			showCityPickerButton.addEventListener('tap', function(event) {
				cityPicker3.show(function(items) {
					cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
					//返回 false 可以阻止选择框的关闭
					//return false;
				});
			}, false);
		}
	});
})(mui, document);

//点击跟换地区
$(document).on('tap', '.region_text', function() {
	var region_text = $(this).text(); //地区值
	var region_text_Country = region_text.split("(")[0];
	$("#region_text_Country").text(region_text_Country);
	var region_text_code = region_text.split("(")[1];
	$("#region_text_code").text("(" + region_text_code);
	var city_leabers = $(this).children("a").attr('leaber_num');
	/*三级联动*/
	(function($, doc) {
		$.init();
		$.ready(function() {
			/**
			 * 获取对象属性的值
			 * 主要用于过滤三级联动中，可能出现的最低级的数据不存在的情况，实际开发中需要注意这一点；
			 * @param {Object} obj 对象
			 * @param {String} param 属性名
			 */
			var _getParam = function(obj, param) {
				return obj[param] || '';
			};

			if(city_leabers == "86") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData3);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "00886") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData5);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "60") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData4);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else if(city_leabers == "00852") {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData6);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);
			} else {
				cityPicker3 = new $.PopPicker({
					layer: 3
				});
				cityPicker3.setData(cityData7);
				var showCityPickerButton = doc.getElementById('region');
				var cityResult3 = doc.getElementById('region');
				showCityPickerButton.addEventListener('tap', function(event) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						//返回 false 可以阻止选择框的关闭
						//return false;
					});
				}, false);

			}

		});
	})(mui, document);
})

$(document).on('tap', '.return', function() {
	localStorage.clear();
	window.top.location.href = "login.html"
})

$(document).on('tap', '.the_login', function() {
	var region_area = $("#region").text();
	if(region_area == "地区") {
		mui.toast("请选择地区");
		return false;
	}
	var region_init = region_area.split(" "); //截取地区
	var receiver_state = region_init[0]; //省
	var receiver_city = region_init[1]; //市
	var receiver_district = region_init[2]; //区
	addAddress(receiver_state, receiver_city, receiver_district); //增加地址
})
//绑定地区
function addAddress(receiver_state, receiver_city, receiver_district) {
	mui.ajax(user_url + '/api/addAddress', {
		data: {
			"receiver_city": receiver_city,
			"receiver_district": receiver_district,
			"receiver_state": receiver_state,
			"sign": "string",
			"user_id": user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		headers: {
			'Content-Type': 'application/json'
		},
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.code == 10000) {
				if(!user_phone) {
					mui.openWindow({
						url: 'certified_cell_phone_num.html',
						extras: {
							user_id: user_id,
							is_realname: is_realname
						},
						createNew: true
					})
				} else if(is_realname == 0) {
					mui.openWindow({
						url: '../my/name_authentication.html',
						createNew: true
					})
				} else {
					window.top.location.href = "../my/my_user.html";
				}
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});

}

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		user_phone = self.my_phone; //手机号码
		is_realname = self.is_realname; //实名状态
	});
}